package web;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.CourseService;
import service.Numbers;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpLoadCourse extends HttpServlet {

    @Override
    public void service(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {
        //设置存储路径
        String uploadPath=request.getServletContext().getRealPath("/image");
        File fatherFile=new File(uploadPath);
        fatherFile.mkdirs();
        //1.文件的上传工厂
        DiskFileItemFactory factory=new DiskFileItemFactory();
        //2.文件上传的核心类
        ServletFileUpload upload=new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        String imagePath=null;
        //3.解析request对象
        HashMap<String,String> map=new HashMap<>();
        try {
            List<FileItem> fileItems=upload.parseRequest(request);
            //如果不是文件内容
            for(FileItem fileItem:fileItems){
                if(fileItem.isFormField()){
                    String fieldName=fileItem.getFieldName();
                    String value=fileItem.getString("UTF-8");
                    System.out.println(fieldName+"="+value);
                    map.put(fieldName,value);
                }
            }
            String courseName=map.get("coursename");
            String introduction=map.get("introduction");
            String tid=request.getSession().getAttribute("username").toString();
            int picNum=CourseService.addCourse(courseName,introduction,tid);
            System.out.println("添加课程"+courseName+"成功");
            imagePath=uploadPath+"/"+picNum+".jpg";
            for (FileItem fileItem:fileItems) {
                if(!fileItem.isFormField()){
                    String name=fileItem.getName();
                    String fieldName=fileItem.getFieldName();
                    System.out.println("name="+name);
                    System.out.println("fieldName="+fieldName);
                    //4.得到上传文件的输入流
                    InputStream inputStream=fileItem.getInputStream();
                    //5. IO流的拷贝操作
                    //5.1先创建一个文件
                    File file=new File(imagePath);
                    //5.2创建输出流
                    FileOutputStream outputStream=new FileOutputStream(file);
                    //5.3设置缓冲区
                    byte[] buffer=new byte[1024];
                    int len;
                    while ((len=inputStream.read(buffer,0, buffer.length))!=-1){
                        outputStream.write(buffer,0,len);
                    }
                    outputStream.flush();
                    inputStream.close();
                    outputStream.close();
                }
            }
            res.getWriter().write("true");
        } catch (FileUploadException e) {
            e.printStackTrace();
            res.getWriter().write("false");
        }

    }
}



